const log = require('./logs.js');
/**
 * 获取地理位置组参加密返回跳转博饼H5地址
 */
const getBobingKey = async function(that,netWork,url){
    log.info('[getBobingKey]--->[获取地理位置组参加密返回跳转博饼H5地址]');
    wx.login({
        success: res => {
            console.log("[onLoad--wx.login]" + res.code);
            if (res.code) {
             wx.setStorageSync('code', res.code)
            } else {
             console.log("获取jsCode失败" + res.errMsg);
            }
        }
    });
    that.isLoading(true, "正在获取位置...");
    let res = await new Promise((resolve, reject)=>{
        wx.getLocation({
            type: 'wgs84',
            success: res => {
                that.isLoading(false)
                resolve(res);
            },
            fail: res => {
                that.isLoading(false)
                log.error('[wx.getLocation===res.errMsg]' + JSON.stringify(res.errMsg))
                resolve(res);
            }
        })  
    })
    if (res.errMsg != "getLocation:ok") {
        that.setData({
            isShowModalAuth: true,
            authText: "为了保证正常获取奖励，现需获取您的地理位置",
            isSetting: 1
        })
        return false;
    }
    log.info('[getBobingKey]--->latitude='+res.latitude+"&longitude="+res.longitude);
    that.isLoading(true, "正在加载...");
    let key = await new Promise((resolve, reject)=>{
        netWork.POST_Bobing('encode', {
            data: {
                latitude: res.latitude,
                longitude: res.longitude,
                userId: wx.getStorageSync('userId')
            },
            success: function (res) {
                that.isLoading(false)
                resolve(res.data);
            },
            fail: function (res) {
                that.isLoading(false)
                resolve(false);
            }
        });
    })
    if(key.code == "1001"){
        return url+"?channel=2&param="+key.data;
    }else{
        wx.showToast({
            title: '操作失败，稍后重试',
            icon: 'none'
        })
        log.error('[getBobingKey]--->[encode error]'+key.code);
        return false;
    }
}
module.exports = {
    getBobingKey
}